package view.GUI.adminView.properties;

import java.util.ArrayList;
import java.util.List;

import model.handler.databaseHandling.QueryFactory;
import enums.GUIText;
import enums.Property;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import view.GUI.mainView.MainView;


public final class PropertiesView extends Application{
	
	private Button btnSave;
	private Button btnCancel;	
	
	private VBox mainVBox;
	
	private GridPane pane;
	
	private ScrollPane sp;
	
	private MainView owner;
	
	private Scene scene;
	private Stage primaryStage;
	
	private List<HBox> propertiesList;
	
	private int rowCounter;
	
	public PropertiesView(){
		rowCounter = 1;
//		this.owner = mainView;
//		primaryStage = new Stage();
		propertiesList = new ArrayList<>();
		createWidgets();
		addWidgets();
		setupLayout();
		setupInteraction();
		
		for(Property p: Property.values()){
			addNewProperty(p);
		}
		
//		start(primaryStage);
	}
	
	private void addNewProperty(Property prop){
		HBox hbox = new HBox();
		Label lbl = new Label(prop.toString());
		TextField txt = new TextField();
		txt.setText(QueryFactory.getProperty(prop));
		hbox.setSpacing(10);
		
		hbox.getChildren().addAll(lbl, txt);
		propertiesList.add(hbox);
		
		pane.add(hbox, 0, rowCounter);
		GridPane.setConstraints(hbox, 0, rowCounter++, 5, 1);
	}
	
	private void createWidgets(){
		mainVBox = new VBox();
		pane = new GridPane();
		
		btnCancel = new Button(GUIText.CANCEL.toString());
		btnSave = new Button(GUIText.SAVE.toString());
	
		sp = new ScrollPane();
	}
	
	private void addWidgets(){
		
		pane.add(mainVBox, 0, 0);
		pane.add(btnSave, 0, 1);
		pane.add(btnCancel, 1, 1);
		
		sp.setContent(pane);
	}
	
	private void setupLayout(){
		pane.setHgap(10);
		pane.setVgap(10);
		pane.setPadding(new Insets(10,10,10,10));
	}
	
	private void setupInteraction(){
		btnCancel.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent arg0) {
				primaryStage.close();
			}
		});
	}

	/**
	 * This method is needed in order to display the Login-Dialog.
	 */
	@Override
	public void start(Stage primaryStage) {

		scene = new Scene(sp);
		this.primaryStage = primaryStage;
		this.primaryStage.initModality(Modality.WINDOW_MODAL);
//		primaryStage.initOwner(owner.getPrimaryStage());
		primaryStage.setScene(scene);
		primaryStage.setTitle(GUIText.PROPERTIES.toString());
		primaryStage.setResizable(true);
		primaryStage.show();
	}
	
	public static void main (String[] args){
		Application.launch();
	}

}
